// 用户--持久层
var mongoose = require('mongoose');

//查询数据
const get = async () => {
    const usersModel=mongoose.model("users");
    return await usersModel
        .find()
        .sort({_id:-1})
        .exec();
};

//新增
const create= async user => {
    await mongoose.model("users").create(user)
};

//更新
const update= async (_id, params) => await mongoose.model("users").update({_id}, params);

//删除
const remove = async (_id) => await mongoose.model("users").remove(_id);

//注册
const reg = async ({ username, password }) => {
   return await mongoose.model("users").create({ username, password });
    // await
};

//登录
const login = async ({ username, password }) =>  {
    return await mongoose.model("users").find({ username, password });
    // await
};

//验证用户名
const isUse = async ({ username }) => {
    return await mongoose.model("users").countDocuments({ username })
    // await
};
module.exports = {create,get,update,remove,reg,login,isUse}